package com.erhuogo.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.TwoCategoryDao;
import com.erhuogo.entity.TwoCategory;
import com.erhuogo.util.DateUtils;

/**
 * 二级分类Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:51:09
 */
public class TwoCategoryDaoImpl extends HibernateDaoSupport implements TwoCategoryDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(TwoCategoryDaoImpl.class);

	// 1.根据一级商品分类Id获取所有启用的二级商品分类信息Dao实现类
	@Override
	public List<TwoCategory> findTwoCategoryByOneId(Integer oneCategoryId) {
		String hql = "from TwoCategory where oneCategoryId = ? and status = ?";
		List<TwoCategory> result = null;
		List<TwoCategory> list = this.getHibernateTemplate().find(hql, oneCategoryId, 0);
		if(list != null && list.size() > 0){
			logger.info("所有启用的二级商品分类集合：" + list);
			result = list;
		}
		return result;
	}

	// 2.分页条件查询指定一级分类下的二级分类的记录数量Dao实现类
	@Override
	public int countByParams(String id, String name, String code, String startDate, String endDate,
			String oneCategoryIdStr) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(TwoCategory.class);
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer twoCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(twoCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<TwoCategory> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的二级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的二级商品分类");
		}
		return count;
	}

	// 3.分页条件查询指定一级分类下的二级分类的记录Dao实现类
	@Override
	public List<TwoCategory> findPageByParams(String id, String name, String code, String startDate, String endDate,
			String oneCategoryIdStr, int startIndex, int pageSize) {
		List<TwoCategory> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(TwoCategory.class);
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer twoCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(twoCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<TwoCategory> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, pageSize);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的二级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的二级商品分类");
		}
		return result;
	}

	// 4.根据ID查询二级分类Dao实现类
	@Override
	public TwoCategory findById(Integer id) {
		String hql = "from TwoCategory where id = ?";
		TwoCategory result = null;
		List<TwoCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list != null && list.size() > 0){
			logger.info("根据ID:"+id+",查询到二级分类:" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	// 5.保存或者更新二级分类信息Dao实现类
	@Override
	public void saveTwoCategory(TwoCategory twoCategory) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(twoCategory);
	}

	// 6.更新二级分类信息状态Dao实现类
	@Override
	public void updateStauts(Integer id, Integer statusCode) throws Exception {
		String hql = "from TwoCategory where id = ?";
		List<TwoCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			TwoCategory twoCategory = list.get(0);
			twoCategory.setStatus(statusCode);
			twoCategory.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(twoCategory);
		}
	}

	// 1.根据一级商品分类Id获取所有的二级商品分类信息Dao实现类
	@Override
	public List<TwoCategory> findAllTwoCategoryByOneId(Integer oneCategoryId) {
		String hql = "from TwoCategory where oneCategoryId = ?";
		List<TwoCategory> result = null;
		List<TwoCategory> list = this.getHibernateTemplate().find(hql, oneCategoryId);
		if(list != null && list.size() > 0){
			logger.info("所有二级商品分类集合：" + list);
			result = list;
		}
		return result;
	}
}
